package com.originskyseed.platform.job.jobhandler.task;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.job.biz.model.ReturnT;
import com.originskyseed.platform.core.job.handler.IJobHandler;
import com.originskyseed.platform.core.job.handler.annotation.JobHandler;
import com.originskyseed.platform.core.job.log.JobLogger;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.task.model.Task;
import com.originskyseed.platform.task.model.TaskRelateAssignPerson;
import com.originskyseed.platform.task.service.ITaskRelateAssignPersonService;
import com.originskyseed.platform.task.service.ITaskService;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IUserInfoService;

/**
 * 新员工自动添加培训任务
 *
 * @Author: wlj @date: 2021/4/20
 */

@Component
@JobHandler(value = "newUserTaskTrainJobHandler")
public class NewUserTaskTrainJobHandler extends IJobHandler {

	@DubboReference
	private ITaskService taskService;

	@DubboReference
	private ITaskRelateAssignPersonService taskRelateAssignPersonService;

	@DubboReference
	private IUserInfoService userInfoService;

	@Override
	public ReturnT<String> execute(String tenantCode) throws Exception {
		try {
			// 查询培训任务
			List<TaskRelateAssignPerson> taskRelateAssignPeopleList = new ArrayList<>();
			List<Task> taskList = this.taskService.selectNewUserTrainTaskJobList(4, tenantCode);
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			if (taskList != null && taskList.size() > 0) {
				for (Task task : taskList) {
					// 查询新用户
					Map<String, Object> map = new HashMap<>();
					if (task.getCreateTime() != null) {
						map.put("createTime", format.format(task.getCreateTime()));
						if (task.getUserType() != null) {
							if (task.getUserType() == 1) {
								map.put("type", 11);
							}
							if (task.getUserType() == 2) {
								map.put("type", 6);
							}
						}
						List<UserInfo> userInfoList = this.userInfoService.taskSelectNewUserList(map, tenantCode);
						if (userInfoList != null && userInfoList.size() > 0) {
							for (UserInfo userInfo : userInfoList) {
								// 校验指派人表是否已经有数据
								TaskRelateAssignPerson taskRelateAssignPerson = this.taskRelateAssignPersonService.selectPaperIsMyTaskTenantCode(task.getId(), userInfo.getId(), tenantCode);
								if (taskRelateAssignPerson == null) {
									TaskRelateAssignPerson assignPerson = new TaskRelateAssignPerson();
									assignPerson.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
									assignPerson.setTaskId(task.getId());
									assignPerson.setState(0);
									assignPerson.setCreateTime(new Date());
									assignPerson.setUserId(userInfo.getId());
									assignPerson.setName(userInfo.getName() == null ? "" : userInfo.getName());
									assignPerson.setAccount(userInfo.getAccount() == null ? "" : userInfo.getAccount());
									if (userInfo.getType() == 6) {
										assignPerson.setUserType(2);
									} else if (userInfo.getType() == 11) {
										assignPerson.setUserType(1);
									}
									taskRelateAssignPeopleList.add(assignPerson);
								}
							}
						}
					}
				}
			}
			if (taskRelateAssignPeopleList != null && taskRelateAssignPeopleList.size() > 0) {
				this.taskRelateAssignPersonService.insertBatch(taskRelateAssignPeopleList, tenantCode);
			}
		} catch (Exception e) {
			JobLogger.log("任务广场新员工自动添加培训任务：" + getExceptionDetail(e));
			return FAIL;
		}
		return SUCCESS;
	}


	/**
	 * 获取异常详细信息，知道出了什么错，错在哪个类的第几行 .
	 *
	 * @param ex
	 * @return
	 */
	public static String getExceptionDetail(Exception ex) {
		String ret = null;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		PrintStream pout = new PrintStream(out);
		try {
			ex.printStackTrace(pout);
			ret = new String(out.toByteArray());
		} catch (Exception e) {
		} finally {
			try {
				pout.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}

}
